<?php

/**
 * Description of class
 *
 * @author ������
 */
class LRuser implements IObservable{

    public $strategy;
    public $user_id;
    public $user_login;
    public $user_hash;
    public $user_ip;
    public $realname;
    public $phone;
    public $mail;
    public $bday;
    public $photo_small;
    public $photo_medium;
    public $photo_big;
    public $ownerof;


    public $isLogged;

    public $LRModel;
    public $view;
    
    public $aryObserversArray;
 
    public $cookies_name_array;
    
    public $registry;
    
    public function __construct() {
        $this->registry = Registry::getInstance();
        $load = $this->registry->load;

        //������ �����
        $this->view = new LRView();
        //�������� LRModel �� registry
        $this->LRModel = new LRModel();

        if (isset($_COOKIE['user_id']) && isset($_COOKIE['hash'])) {
            $userdata = $this->LRModel->getUserDataById($_COOKIE['user_id']);

            if (($userdata['user_hash'] !== $_COOKIE['hash']) || ($userdata['user_id'] !== $_COOKIE['user_id'])) {
                setcookie("user_id", "", time() - 3600 * 24 * 30 * 12, "/");
                setcookie("hash", "", time() - 3600 * 24 * 30 * 12, "/");
                $this->isLogged = false;
            } else {
                $this->isLogged = true;
                $this->user_id = $userdata['user_id'];
                $this->user_login = $userdata['user_login'];
                $this->user_hash = $userdata['user_hash'];
                $this->user_ip = $userdata['user_ip'];
                $this->realname = $userdata['realname'];
                $this->phone = $userdata['phone'];
                $this->mail = $userdata['mail'];
                $this->bday = $userdata['bday'];
                $this->photo_small = $userdata['photo_small'];
                $this->photo_medium = $userdata['photo_medium'];
                $this->photo_big = $userdata['photo_big'];
                $this->ownerof = $userdata['ownerof'];
            }
        } else {
            $this->isLogged = false;
        }


        $this->cookies_name_array = array();
        $this->cookies_name_array["vk"] = "vk_app_" . VK_APP_ID;
        //.....etc..
    }
    
    //������� �������, ��������� � ������� ��� ������������ ���������.
    public function fireEvent($eventType, $data) {

        if (is_array($this->aryObserversArray)) {
            foreach ($this->aryObserversArray as $objObserver) {
                $objObserver->notify($eventType, $data);
            }
        }
    }

    public function addObserver(IObserver $obs)
    {
       $this->aryObserversArray[] = $obs;
    }
    //������ ������
    public function registerUser() {
        if (isset($_POST['submit'])) {
            $errors = array();
            if (!preg_match("/^[a-zA-Z0-9]+$/", $_POST['login'])) {

                $errors[] = "В логине найдены не допустимые символы";
            }
            if (strlen($_POST['login']) < 3 or strlen($_POST['login']) > 30) {
                $errors[] = "Длина логина должна быть не меньше 3 и не больше 30-и сиволов";
            }
  
            if ($this->LRModel->hasUserInBase($_POST['login'])) {
                $errors[] = "Такой пользователь уже есть в базе";
            }

            if (count($errors) == 0) {
                $login = $_POST['login'];

                $password = md5(md5(trim($_POST['password'])));

                $this->fireEvent('message', array('message'=>"Регистрация успешна!"));
                
                $this->LRModel->addUser($login, $password);
                //echo '<a href = "'."http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">�����</a><br>';
                return true;
            } else {
                $this->fireEvent('message', array('message'=>"<b>Найдены ошибки в ходе регистрации:</b>"));
                foreach ($errors AS $error) {
                    
                    $this->fireEvent('message', array('message'=>"$error"));
                }
                //echo '<a href = "'."http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'].'">�����</a><br>';
                return false;
            }
        } else {
            return false;
        }
    }

    public function setStrategy(LoginStrategy $strategy) {
        $this->strategy = $strategy;
    }

    public function doLogin() {
        return $this->strategy->doLogin($this);
    }
    public function doLogout() {
                setcookie("user_id", "", time() + 60 * 60 * 24 * 30, "/");
                setcookie("hash", "", time() + 60 * 60 * 24 * 30, "/");
                
                $this->isLogged = false;
    }

}

?>
